<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;

function themeConfig($form) {
    $logoUrl = new Typecho_Widget_Helper_Form_Element_Text('logoUrl', NULL, NULL, _t('站点 LOGO 地址'), _t('在这里填入一个图片 URL 地址, 以在网站标题前加上一个 LOGO'));
    $form->addInput($logoUrl);
    
    $sidebarBlock = new Typecho_Widget_Helper_Form_Element_Checkbox('sidebarBlock', 
    array('ShowRecentPosts' => _t('显示最新文章'),
    'ShowRecentComments' => _t('显示最近回复'),
    'ShowCategory' => _t('显示分类'),
    'ShowArchive' => _t('显示归档'),
    'ShowOther' => _t('显示其它杂项')),
    array('ShowRecentPosts', 'ShowRecentComments', 'ShowCategory', 'ShowArchive', 'ShowOther'), _t('侧边栏显示'));
    
    $form->addInput($sidebarBlock->multiMode());
}

function themeFields($layout) {
    $logoUrl = new Typecho_Widget_Helper_Form_Element_Text('logoUrl', NULL, NULL, _t('文章主图'), _t('在这里填入一个图片URL地址, 作为文章的主图'));
    $layout->addItem($logoUrl);
    $recommend = new Typecho_Widget_Helper_Form_Element_Text('recommend', NULL, NULL, _t('推荐'), _t('1代表推荐'));
    $layout->addItem($recommend);
}

/**
 * 根据文章的自定义字段 recommend 来筛选推荐的文章
 * Class Widget_Post_Recommend
 */
class Widget_Post_Recommend extends Widget_Abstract_Contents
{
    public function __construct($request, $response, $params = NULL)
    {
        parent::__construct($request, $response, $params);
        $this->parameter->setDefault(
            array(
                'pageSize' => 5,
                'parentId' => 0,
                'ignoreAuthor' => false
            )
        );
    }
    public function execute()
    {
        $query = $this->db->select()->from('table.fields')
            ->where('table.fields.name = ?', 'recommend')
            ->where('table.fields.str_value = ?', '1');
        $results = $this->db->fetchAll($query);
        $ids = [0];
        foreach ($results as $result) {
            $ids []= $result['cid'];
        }
        $query = $this->db->select()->from('table.contents')
            ->where('cid in ?', $ids)
            ->where('table.contents.status in ?', ['publish', 'hidden'])
            ->order('modified', Typecho_Db::SORT_DESC);
        $this->db->fetchAll($query, array($this, 'push'));
    }
}

/**
 * 根据独立页面的自定义字段 showInNav 来筛选页面
 * Class Widget_Page_ShowInNav
 */
class Widget_Page_ShowInNav extends Widget_Abstract_Contents
{
    public function __construct($request, $response, $params = NULL)
    {
        parent::__construct($request, $response, $params);
    }

    public function execute()
    {
        $query = $this->db->select()->from('table.fields')
            ->where('table.fields.name = ?', 'showInNav')
            ->where('table.fields.str_value = ?', '1');
        $results = $this->db->fetchAll($query);
        $ids = [0];
        foreach ($results as $result) {
            $ids []= $result['cid'];
        }
        $query = $this->db->select()->from('table.contents')
            ->where('cid in ?', $ids)
            ->where('table.contents.type = ?', 'page');
        $this->db->fetchAll($query, array($this, 'push'));
    }
}